package example;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.rmi.RemoteException;

//public class OutsourceTask extends Thread{
public class OutsourceTask {
	public MiniTask mini;
	String host = "";
	MixProtocol msg ;
	public String localQuery;
	ObjectOutputStream basedOut;
	boolean controlSite = false;
	boolean interSite = false;
	boolean remoteSite = false;
	Socket senderSock;
	ObjectOutputStream outInter;
	ObjectInputStream inInter;
	public int join = 0;
//	public OutsourceTask(MiniTask mini1,SocketList senderSocket, String host,MixProtocol msg1,String localQuery1,ObjectOutputStream bout, int type, int joinType) throws RemoteException, Exception {
//		System.out.println("OutSource task begin at host:" + host + " remotequery = " +msg1.remotesiteQuery + " result size = " +msg1.result.size());
//		mini = new MiniTask(localQuery1);
//		mini = mini1;
//		this.host = host;
//		msg = new MixProtocol();
//		msg.joinNode1Condition = msg1.joinNode1Condition;
//		msg.joinNode2Condition = msg1.joinNode2Condition;
//		msg.controlsiteQuery = msg1.controlsiteQuery;		
//		msg.remotesiteQuery = msg1.remotesiteQuery;
//		msg.intersiteQuery = msg1.intersiteQuery;
//		msg.remoteSite = msg1.remoteSite;
//		msg.interSite = msg1.interSite;
//		msg.JoinNode1 = msg1.JoinNode1;
//		msg.JoinNode2 = msg1.JoinNode2;
//		msg.result = msg1.result;
//		msg.type = msg1.type;
//		this.localQuery = localQuery1;
//		this.senderSock = senderSocket.socketId;
//		basedOut = bout;
//		if(type==MixProtocol.QUERY) interSite = true;
//		else if (type == MixProtocol.REMOTE) remoteSite = true;
//		else if (type == MixProtocol.JOIN) controlSite = true;
//		outInter = senderSocket.outInter;
//		inInter = senderSocket.inInter;
//		join = joinType;
//	}
//	public void run() {
//		if(controlSite) {
//			this.forwardandJoin();
//		}
//		else if(interSite) {
//			try {
//				this.interSiteJob();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//	}
//	public void forwardandJoin() {
//		//controlSite->interSite
//		try {
//			MixProtocol rst = new MixProtocol();
//			
//			if (msg != null) {
//				outInter.writeObject(msg);
//			}		
//			if ((rst = (MixProtocol)inInter.readObject()) != null) {		
//	    		MixProtocol rtr = new MixProtocol();
//	    		while(mini.getResult().size()==0) {
//	    			int i=0;
//	    			i++;
//	    		}	
//	    		if (join==1)
//	    			mini.localResult = mini.dbConn.joinResult(mini.getResult(),rst.result,0,msg.joinNode1Condition);
//	    		else
//	    			mini.localResult = mini.dbConn.joinResult(mini.getResult(),rst.result,0,msg.joinNode2Condition);
//	    		if(interSite)
//	    			System.out.println("X sends " + msg.interSite + " "+ mini.localResult.size());
//	    		else
//	    			System.out.println("X sends "+ msg.remoteSite + " "+ mini.localResult.size());
//	    		//basedOut.writeObject(rtr);
//			}	
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//	}
//	public void forward() {		
//		String interHost = "";
//		try {
//			interHost = util.lookupHost(this.host);		
//			MixProtocol rst = new MixProtocol();			
//			if (msg != null) {
//				outInter.writeObject(msg);
//			}
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		} catch (Exception e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
//	}
//	public void interSiteJob() throws Exception {
//		System.out.println("Inter--> remote == "+msg.remoteSite);
//		MixProtocol msg = new MixProtocol();
//		msg.type = MixProtocol.REMOTE;
//		MixProtocol rst = new MixProtocol();
//		ObjectOutputStream outRemote = new ObjectOutputStream(senderSock.getOutputStream());
//		ObjectInputStream inRemote = new ObjectInputStream(senderSock.getInputStream());
//		try {	    		
//    		if (msg != null) {
//    			outRemote.writeObject(msg);
//    			System.out.println("INTER->>REMOTE");
//    		}
//    	}	
//    	catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}	
//    	try{
//			if ((rst = (MixProtocol)inRemote.readObject()) != null) {		
//				System.out.println("remoteSite executing "+mini.getResult().size());
//	    		MixProtocol rtr = new MixProtocol();
//	    		rtr.type = MixProtocol.RESULT;
//	    		if(join==1) 
//	    			rtr.result = mini.dbConn.joinResult(mini.getResult(),rst.result,0,msg.joinNode1Condition);
//				else 
//					mini.localResult = mini.dbConn.joinResult(mini.getResult(),rst.result,0,msg.joinNode2Condition);
//				System.out.println("INTER -> CONTROL");
//	    		String content = "";
//	    		for(int i=0;i<rtr.result.size();i++) {
//	    			Tuples filedValue = new Tuples();
//	    			filedValue =  rtr.result.get(i);
//	    			content += filedValue.tups.get(0)+";";
//	    		}
//	    		basedOut.writeObject(rtr);
//	    		System.out.println(content);
//			}				
//		}
//		catch (Exception e) {
//			System.out.println(e.getMessage());
//		}  
//	}
	
	private void remoteSite() {
		// TODO Auto-generated method stub
		
	}
}
